//
//  COGuestController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/7.
//

import UIKit

class COGuestController: UIViewController {
    
    lazy var bgImg: UIImageView = {
        let nav = UIImageView()
        nav.image = KImaged("coco_main_bg")
        nav.contentMode = .scaleAspectFill
        nav.clipsToBounds = true
        return nav
    }()
    
    private lazy var logo: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "logo"))
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .bold(25)
        label.textColor = .white
        label.text = "TAGI"
        return label
    }()
    
    private lazy var userLoginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Start", for: .normal)
        btn.titleLabel?.font = .semibold(24)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var delegateLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.rz.colorfulConfer { confer in
            confer.text("By continuing, You agree to \nour ")?.font(.regular(13)).textColor(UIColor.colorWithHex("#C8BCD3"))
            confer.text("Terms of Service")?.font(.regular(14)).textColor(.rgba(255, 77, 149)).tapActionByLable("terms")
            confer.text(" and ")?.font(.regular(13)).textColor(UIColor.colorWithHex("#C8BCD3"))
            confer.text("Privacy Policy")?.font(.regular(14)).textColor(.rgba(255, 217, 83)).tapActionByLable("privacy")
        }
        label.rz.tapAction {[weak self] label, tapActionId, range in
            var url = ""
            if tapActionId == "terms" {
                url = KTerms
            } else if tapActionId == "privacy" {
                url = KPolicy
            }
            let vc = COWebViewController(url)
            self?.navigationController?.pushViewController(vc)
        }
        return label
    }()
    
    lazy var launchMp4: COLaunchView = {
        let launch = COLaunchView(frame:CGRect(x: 0, y: 0, width: SCREENWIDTH, height: SCREENHEIGHT))
        return launch
    }()
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        DispatchQueue.main.safeAsync {
            self.userLoginButton.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range:PTW(300))
            self.userLoginButton.clipsAllCorners(radius: PTW(65/2))
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(self.bgImg)
        self.bgImg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.view.addSubview(self.logo)
        self.logo.snp.makeConstraints { make in
            make.size.equalTo(PTW(44))
            make.left.equalToSuperview().offset(PTW(20))
            make.top.equalToSuperview().offset(KStatusAndNavBarHeight - PTW(44))
        }
        
        self.view.addSubview(self.nameLabel)
        self.nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.logo)
            make.left.equalTo(self.logo.snp.right).offset(PTW(5))
        }
        
        self.view.addSubview(self.delegateLabel)
        delegateLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-bottomSafeHeight-PTW(40))
        }
        
        self.view.addSubview(self.userLoginButton)
        self.userLoginButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(PTW(300), PTW(65)))
            make.bottom.equalTo(delegateLabel.snp.top).offset(-PTW(35))
            make.centerX.equalToSuperview()
        }
        guard kStaticStartStr.keyGetNSDefaultsKey() != nil else {
            return
        }
        self.view.insertSubview(self.launchMp4, at: 1)
    }
    
    @objc private func clickButton(_ btn: UIButton) {
        var param: [String:Any] = [:]
        param["loginId"] = HDeviceIdentifier.deviceIdentifier()
        param["age"] = 20
        self.view.makeToastActivity(.center)
        COHttpManager.request(target: COBaseApi.guestRegister(param: param), modelType: COUserInfoResponseModel.self) { code, object, success in
            self.view.hideToast()
            COUserInfoManager.configLoginData(response: object)
            KAppDelegate.setRootViewController()
        } failureBlock: { code, msg in
            self.view.hideToast()
        }
    }
}
